HomePromotion.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. "use client";
  2. import { PromotionRep } from "@/api/home";
  3. import Box from "@/components/Box";
  4. import { Mask } from "antd-mobile";
  5. import { FC, MouseEvent, useEffect, useState } from "react";
  6. import dayjs from "dayjs";
  7. import Image from "next/image";
  8. import { Pagination } from "swiper/modules";
  9. import { Swiper, SwiperSlide } from "swiper/react";
  10. interface Props {
  11. data?: PromotionRep[];
  12. type?: 1 | 2; // 每天只弹一次 / 每次登录都弹
  13. }
  14. const HomePromotion: FC<Props> = (props) => {
  15. const { data, type } = props;
  16. const [visible, setVisible] = useState(false);
  17. useEffect(() => {
  18. const isClosePromotion = sessionStorage.getItem("isClosePromotion");
  19. // 如果 type 为 1 && 有isNow为true 表示已经弹出,
  20. const shouldShowPromotion = () => {
  21. // 如果不等于1而数据又是时间,清除
  22. if (type !== 1 && dayjs().isSame(isClosePromotion, "days")) {
  23. sessionStorage.removeItem("isClosePromotion");
  24. return true;
  25. }
  26. if (type === 1) {
  27. return !dayjs().isSame(isClosePromotion, "days");
  28. } else if (type === 2) {
  29. return !isClosePromotion;
  30. }
  31. return false;
  32. };
  33. let flag = shouldShowPromotion();
  34. setVisible(flag);
  35. }, []);
  36. const closeHandler = (e: MouseEvent<HTMLElement>) => {
  37. e.stopPropagation();
  38. setVisible(false);
  39. if (type === 1) {
  40. sessionStorage.setItem("isClosePromotion", dayjs().format("YYYY-MM-DD"));
  41. }
  42. if (type === 2) {
  43. sessionStorage.setItem("isClosePromotion", "true");
  44. }
  45. };
  46. if (data && data.length === 0) return null;
  47. return (
  48. <div>
  49. <Mask visible={visible} onMaskClick={(e) => closeHandler(e)}>
  50. <div
  51. className={
  52. "promotion-swiper absolute left-1/2 top-[18%] max-w-[3.139rem]" +
  53. " -translate-x-1/2"
  54. }
  55. >
  56. {/*<div*/}
  57. {/* style={{*/}
  58. {/* clipPath: "polygon(26% 0, 100% 69%, 100% 0)",*/}
  59. {/* background: "rgba(0,0,0,0.5)",*/}
  60. {/* }}*/}
  61. {/* className={*/}
  62. {/* "absolute right-[0.1111rem] top-[0.0694rem] z-[10] h-[0.5556rem] w-[0.5556rem]" +*/}
  63. {/* " rounded-tr-[0.1389rem]"*/}
  64. {/* }*/}
  65. {/*>*/}
  66. {/* <i className={"iconfont icon-guanbi absolute right-[20%] top-[5%]"}></i>*/}
  67. {/* /!*<i className={"iconfont icon-guanbi"}></i>*!/*/}
  68. {/* /!*<div*!/*/}
  69. {/* /!* onClick={closeHandler}*!/*/}
  70. {/* /!* className={*!/*/}
  71. {/* /!* "z-20 h-0 w-0 [border-width:0_1.3889rem_1.3889rem_0]" +*!/*/}
  72. {/* /!* " [border-color:transparent_#007bff_transparent_transparent]" +*!/*/}
  73. {/* /!* " [border-color: red] flex"*!/*/}
  74. {/* /!* }*!/*/}
  75. {/* /!*>*!/*/}
  76. {/* /!* <i className={"iconfont icon-guanbi ml-[0.3472rem] mt-[0.0694rem]"}></i>*!/*/}
  77. {/* /!*</div>*!/*/}
  78. {/*</div>*/}
  79. {/*<i className={"iconfont icon-guanbi"}></i>*/}
  80. <Swiper
  81. loop
  82. pagination={{ clickable: true }}
  83. spaceBetween={10}
  84. scrollbar={{ draggable: true }}
  85. modules={[Pagination]}
  86. slidesPerView={1}
  87. grabCursor={true}
  88. navigation={true}
  89. className={"min-h-[3.8rem]"}
  90. >
  91. {data?.map((promotion, index) => (
  92. <SwiperSlide key={index}>
  93. <Box
  94. action={promotion.action_type}
  95. actionData={promotion.action_params}
  96. >
  97. <div
  98. onClick={(e) => closeHandler(e)}
  99. className={
  100. "relative h-[3.4722rem] w-[100%] rounded-[0.1389rem]"
  101. }
  102. >
  103. <div
  104. style={{
  105. clipPath: "polygon(26% 0, 100% 69%, 100% 0)",
  106. background: "rgba(0,0,0,0.5)",
  107. }}
  108. className={
  109. "absolute -top-[0.2px] right-0 z-[10]" +
  110. " h-[0.5556rem] w-[0.5556rem]" +
  111. " rounded-tr-[0.1389rem]"
  112. }
  113. >
  114. <i
  115. className={
  116. "iconfont icon-guanbi absolute right-[16%]" +
  117. " top-[5%] text-[0.1111rem]"
  118. }
  119. ></i>
  120. </div>
  121. <Image
  122. width={400}
  123. height={400}
  124. className={"h-[100%] w-[100%] rounded-[0.1389rem]"}
  125. src={promotion.content.image!}
  126. alt={promotion.content.title!}
  127. />
  128. </div>
  129. </Box>
  130. </SwiperSlide>
  131. ))}
  132. </Swiper>
  133. </div>
  134. </Mask>
  135. </div>
  136. );
  137. };
  138. export default HomePromotion;